package com.sss.service.impl;

import com.sss.dao.AdminMapper;
import com.sss.entity.Admin;
import com.sss.service.AdminService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Transactional
@Service("adminService")
public class AdminServiceImpl implements AdminService {
    protected Logger logger= LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    private AdminMapper adminMapper;

    @Resource(name = "adminMapper")
    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Admin login(String username, String password) {
        return adminMapper.selectByLogin(username,password);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean update(Admin admin) {
        return adminMapper.updateOne(admin)>0;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean chagepassword(String lastpassword, String newpassword) {
        Admin admin=login("admin",lastpassword);
        if ( admin==null){
            return false;
        }else {
            logger.info("last"+lastpassword+"newpass"+newpassword);
            Admin admin1 = new Admin();
            admin1.setPassword(newpassword);
            admin1.setId(1);
            update(admin1);
            return true;
        }
    }
}
